class Solution {
public:
    bool dfs(TreeNode* root, int curSum, int target){
        if(root == nullptr) return false;
        curSum += root->val;
        if(root->left == nullptr && root->right == nullptr){
            if(curSum == target) return true;
        }
        return dfs(root->left, curSum, target) || dfs(root->right, curSum, target);
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        return dfs(root, 0, targetSum);
    }
};